﻿/* Pomoho UI - Manage */
/* Created By sorrycc
 * modify by oneMax at 2008-10-28*/
var ifile = 0;
var filePos = new Array("-1","-1","-1","-1","-1","-1");
Pui.photo={
	_flag:0,
	checkFileExt:function(ext){
		var ret = false;
			var fileExt = new Array('jpg','gif','png','bmp');
			for( i = 0 ; i < fileExt.length ; i ++ )
			{
				if( ext.toLowerCase() == fileExt[i] )
				{
					ret = true;
				}
			}
			return ret;
	},
	checkSize:function(imgno){
		var img=new Image()
			try{
				var filedir = document.getElementById("filedir"+imgno).value.trim();
				if( filedir == "" ){return;}
				try
				{
					img.src = filedir; //event.srcElement.value;
				}
				catch(e){alert("对不起,该图片不能预览!");}
				var arr = filedir.split(".");
				if( !this.checkFileExt(arr[arr.length-1]))
				{
					Pui.photo.showDiv('AUploadType');//alert("图片文件格式不正确,只支持jpg,gif或png");
					return;
				}
				else
				{
					ifile = ifile + 1;
					filePos[imgno-1] = "1";
				}
				document.getElementById("img"+imgno).src=filedir;//event.srcElement.value;
				document.getElementById("img"+imgno).width=Pui.photo.getImgSize(img.width,img.height,1);
				document.getElementById("img"+imgno).height=Pui.photo.getImgSize(img.width,img.height,2);
				//alert(document.getElementById("img"+imgno).src+":"+document.getElementById("img"+imgno).width+":"+document.getElementById("img"+imgno).height+":"+img.src+":"+img.width+":"+img.height);
			}catch(e){alert("对不起,格式检查出错!");}
	},
	getImgSize:function(width,height,type){//120*90 type=1返回width type=2返回height
		ret = 0;
			if(width<120 && height<90){
				ret = type==1 ? width:height;
				if( width == 0 || height == 0 ){ ret = type==1 ? 120:90; }
			}
			if(width>120 || height>90){
				var size = width/120.00>height/90.00 ? width/120.00:height/90.00;
				ret = type==1 ? width/size:height/size;
			}
			return ret;
		
	},
	hideDiv:function(divid){
		$('#'+divid+'').hide();  //关闭层
		$('#MarkID').hide();//关闭大层
		if( divid == 'AlbumCreate'){document.getElementById("span_errmsg").innerHTML = "";}
	},
	showDiv:function(divid){
		$('#'+divid+'').showDiv();
	},
	getLength:function(obj){
		var moji = obj.value;
		var i,cnt = 0;
		for(i=0; i<moji.length; i++) if (escape(moji.charAt(i)).length >= 4 ) cnt+=2;
		else cnt++;

		return cnt;
	},
	createAlbum:function(){
		if( document.getElementById("albumName").value == ""){
			document.getElementById("span_errmsg").innerHTML = "请输入相册名称";
			document.getElementById("albumName").focus();
			return;
		}
			
		if( Pui.photo.getLength(document.getElementById("albumName")) > 20 ){
			document.getElementById("span_errmsg").innerHTML = "相册名称不能超过10个汉字";
			document.getElementById("albumName").focus();
			return;
		}
			
			uploadphoto.CreateAlbum(document.getElementById("albumName").value,Pui.photo.createAlbumCallBack);
	},
	createAlbumCallBack:function(res){
		if(res.error != null){
			document.getElementById("span_errmsg").value = res.error;
		}else{
			if( res.value == "-1"){
				document.getElementById("span_errmsg").innerHTML = "该相册已经存在";
				document.getElementById("albumName").focus();
			}
			if( res.value == "-2"){
				document.getElementById("span_errmsg").innerHTML = "数据插入出错";
			}
					
			if( res.value > 0 ){
				if(Pui.photo._flag==1){
					document.getElementById("albumName").value="";
					window.location.reload();
					return
				}
				var obj = document.getElementById('selAlbumList');
				obj.options.add(new Option(document.getElementById("albumName").value,res.value)); 
				obj.selectedIndex = obj.options.length-1;
				document.getElementById("albumName").value = "";
				Pui.photo.hideDiv('AlbumCreate');
			}
		}
	},
	saveAlbumImg:function(){
		if( document.getElementById("uploadids").value != "")
			{
				uploadphoto.SaveAlbumImg(document.getElementById("uploadids").value,document.getElementById("selAlbumList").value,Pui.photo.saveAlbumImgCallBack);
			}
	},
	saveAlbumImgCallBack:function(res){
		if(res.error == null){
			if( res.value > 0){
				Pui.photo.redirecturl(res.value);
				/*$('#PicUploading').hide();
				Pra.ui.overlay();
				$('#PicUploaded p').text("文件上传全部完成");
				$('#PicUploaded').show(10, function(){$('#PicUploaded').dialog({overlay:true,bgColor:'black',opacity:0.5});});*/
			}
		}else{
			alert(res.error);
		}	
	},
	redirecturl:function(albumID){
		if( document.getElementById("uploadids").value != "")
		{
			document.getElementById("uploadids").value = "";
			var albumid = document.getElementById("selAlbumList").value;
			if( albumid <= 0 ){ albumid = albumID; }
			var albumname = document.getElementById("selAlbumList").options[document.getElementById("selAlbumList").selectedIndex].text;
			var url = 'http://b.pomoho.com/manage/manageimg.aspx?albumid='+albumid+'&albumname='+escape(albumname);
			location.href = url;
		}
	},
	getFileindex:function (posVal){
		var ret = "-1";
		for(i=posVal;i<=5;i++)
		{
			if( filePos[i] == "1" )
			{
				ret = i;
				break;
			}
		}
		return ret;
	}
};		
/*文件上传类*/
Pui.PMHUpload = {
	loadURL : function ( url ){
		url = url + "&ran=" + Math.random();
		var script = document.createElement('script');  
		script.type = 'text/javascript'; 
		script.src = url;  
		document.getElementsByTagName('head')[0].appendChild( script ); 
	},
	timer : null,
	uploadGUID : null,
	//进度条URL
	progressURL : "http://b.pomoho.com/pmhupload_progress.ashx",
	currentUploadForm : null,
	uploadForms : null,
	currentUploadIndex : 0,
	//获取GUID
	getUploadGUID : function(){
		var iCurrentFileSN = parseInt(Pui.PMHUpload.currentUploadIndex) + 1;
		document.getElementById("UP1").innerHTML = iCurrentFileSN;
		document.getElementById("UP2").innerHTML = 0 + "%";
		$('#UP2').text(0 + "%");
		$('#UP2').css('width', 0 + "%");
		document.getElementById("UP4").innerHTML = 0;
		document.getElementById("UP5").innerHTML = 0;
		document.getElementById("UP6").innerHTML = 0;
		document.getElementById("UP7").innerHTML = 0;
		var url = Pui.PMHUpload.progressURL + "?action=getguid&callback=Pui.PMHUpload.getUploadGUIDCallBack";
		Pui.PMHUpload.loadURL( url );	
		//var uploadGUID="c34a1d93-5bfc-4f56-b71b-fa94fb252f4a"; 
		//Pui.PMHUpload.getUploadGUIDCallBack(uploadGUID);
	},
	getUploadGUIDCallBack : function( guid ){
		Pui.PMHUpload.uploadGUID = guid;
		var form = Pui.PMHUpload.uploadForms[Pui.PMHUpload.currentUploadIndex];
		Pui.PMHUpload.getInput( form, "uploadid" ).value = guid;		
		//form.getElementsByTagName("INPUT")[0].value = guid;
		form.submit();
		Pui.PMHUpload.clearTimer();
		Pui.PMHUpload.timer = setInterval( Pui.PMHUpload.updateProgress, 200 );
	},
			
	//停止定时器
	clearTimer : function(){
		if( Pui.PMHUpload.timer!=null ){
			clearInterval( Pui.PMHUpload.timer );
			Pui.PMHUpload.timer = null;
		}
	},
			
	//停止当前正在上传文件
	cancelUpload : function(){
		document.getElementById("ifrUpload").src = "about:blank";
		var url = Pui.PMHUpload.progressURL + "?action=cancel&uploadid=" + Pui.PMHUpload.uploadGUID;
		Pui.PMHUpload.loadURL( url );
	},

	//定时更新进度条
	updateProgress : function(){
		var url = Pui.PMHUpload.progressURL + "?action=getinfo&uploadid="+Pui.PMHUpload.uploadGUID+"&callback=Pui.PMHUpload.updateProgressCallBack";
		Pui.PMHUpload.loadURL( url );
		//var status=new Object(); status.fileName="1"; status.leftTime="12:00:00"; status.totalSize="96757"; status.percent="40"; status.speed="706"; status.state="uploading"; status.url="http://pic.pomoho.com/photos/200810/633608059389218750_78780135.jpg"; status.uploadFileID="261080"; status.tempURL="http://fileupload.pomoho.com/tempfiles/200810/633608059389218750_78780135.jpg"; status.message=""; Pui.PMHUpload.updateProgressCallBack(status);
	},
	updateProgressCallBack : function( status ){
				//
		var progressPanel = document.getElementById("progress");
				//		
		var html = "";
		switch( status.state.toLowerCase() ){
			case "uploading" :
				if(status.totalSize/(1024*1024)>10){
					Pui.photo.showDiv('AUploadBig');
					Pui.PMHUpload.cancelUpload();
					return;
				}
				var iCurrentFileSN = parseInt(Pui.PMHUpload.currentUploadIndex) + 1;
				document.getElementById("UP1").innerHTML = iCurrentFileSN;
				if( status.percent > 0 )
				{
					document.getElementById("UP2").innerHTML = status.percent + "%";
					$('#UP2').text(status.percent + "%");
					$('#UP2').css('width', status.percent + "%");
					document.getElementById("UP4").innerHTML = status.leftTime;
					document.getElementById("UP5").innerHTML = Math.round(status.speed/1024);
					document.getElementById("UP6").innerHTML = Math.round((status.totalSize/1024)*(status.percent/100));
					document.getElementById("UP7").innerHTML = Math.round(status.totalSize/1024);
				}
				break;
						
			case "uploaded" :
				document.getElementById("uploadids").value = document.getElementById("uploadids").value+","+status.uploadFileID;
				Pui.PMHUpload.uploadNextFile();
			break;
						
			case "canceled" : 
				Pui.PMHUpload.uploadNextFile();
				break;
						
			case "error" : 
				//html += "上传出错!"
				//progressPanel.innerHTML = html;
				Pui.PMHUpload.uploadNextFile();
			break;
		}
				//
	},
	//上传下一个表单中的文件
	uploadNextFile : function(){
		Pui.PMHUpload.clearTimer();
		Pui.PMHUpload.currentUploadIndex++;
		var iindex = Pui.photo.getFileindex(Pui.PMHUpload.currentUploadIndex);
		if( iindex > 0 )
		{
			Pui.PMHUpload.currentUploadIndex = iindex;
			Pui.PMHUpload.beginUpload();
		}else {
			//所有图片上传完成
			Pui.photo.saveAlbumImg();
			ifile = 0;
		}
				
		/*if( PMHUpload.currentUploadIndex<PMHUpload.uploadForms.length ){
			PMHUpload.beginUpload();
		} else {
			//所有图片上传完成
			saveAlbumImg();
			ifile = 0;
			}*/
	},

	beginUpload : function(){
		var form = Pui.PMHUpload.uploadForms[Pui.PMHUpload.currentUploadIndex];
		var fileName = Pui.PMHUpload.getInput( form, "file" ).value;
		var arr = fileName.split(".");
		if( fileName!="" && Pui.photo.checkFileExt(arr[arr.length-1])){
			Pui.PMHUpload.getUploadGUID();
		} else {
			Pui.PMHUpload.uploadNextFile();
		}
	},
			
	getInput : function( form, name ){
		var ret = null;		
		var inputs = form.getElementsByTagName("INPUT");
		for(  var i=0; i<inputs.length; i++ ){
			if( inputs[i].name.toLowerCase()==name ){
				return inputs[i];
				break;
			}
		}
		return ret;
	},

	//初始化
	init : function(){
		if(ifile>0)
		{
			var uploadPanel = document.getElementById("uploadPanel");
			Pui.PMHUpload.uploadForms = uploadPanel.getElementsByTagName( "FORM" );
			Pui.PMHUpload.currentUploadIndex = 0;
			Pui.PMHUpload.currentUploadIndex = Pui.photo.getFileindex(Pui.PMHUpload.currentUploadIndex);
			Pui.PMHUpload.beginUpload();
			$('#PicUploading').show(10, function(){$('#PicUploading').showDiv();});
		}else{
			Pui.photo.showDiv('AUploadSel');
		}
	}
}
	
		
		
		
